package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbAddressService;
import com.dongdongshop.service.TbItemService;
import com.dongdongshop.service.TbOrderService;
import com.dongdongshop.service.TbSellerService;
import com.dongdongshop.vo.AddressVo;
import com.dongdongshop.vo.CartVo;
import com.dongdongshop.vo.OrderVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

import java.math.BigDecimal;
import java.util.*;

@Service
@DubboService
public class TbOrderServiceImpl implements TbOrderService {
    @Autowired
    private TbOrderMapper tbOrderMapper;

    @DubboReference
    private TbAddressService addressService;

    @DubboReference
    private TbSellerService sellerService;

    @Autowired
    private TbOrderItemMapper orderItemMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @DubboReference
    private TbItemService tbItemService;

    @Override
    @Transactional
    public double addOrder(String[] itmeId, Long userId, TbUser user, Long id) {
        List<CartVo> cartList = (List<CartVo>) redisTemplate.boundHashOps("cartList"). get(user.getId());
        AddressVo address = addressService.selectAddressByAId(userId);
        double he = 0;
        for (CartVo cartVo : cartList){//商家
            TbOrder order = new TbOrder();
            double money = 0;
            for (TbOrderItem orderItem : cartVo.getOrderItemList()) {//商家的明细
                for (String s : itmeId) {
                    if (Objects.equals(orderItem.getItemId().toString(),s)) {//判断
                        order.setUserId(user.getUsername());
                        order.setPaymentType("1");
                        order.setStatus("1");
                        order.setCreateTime(new Date());
                        order.setUpdateTime(new Date());
                        order.setReceiverAreaName(address.getProvinceName() + " " + address.getCityName() + " " + address.getTownName()  + " " + address.getAddress());
                        order.setReceiverMobile(address.getMobile());
                        order.setReceiver(address.getContact());
                        order.setSourceType("1");
                        order.setSellerId(orderItem.getSellerId());
                        BigDecimal totalFee = orderItem.getTotalFee();
                        double v = totalFee.doubleValue();
                        money += v;
                    }
                }
            }
            if (order != null && money > 0){
                IdWorker idWorker = new IdWorker(0,0);
                long l = idWorker.nextId();
                he += money;
                order.setOrderId(l);
                order.setOrderNum(String.valueOf(id));
                order.setPayment(BigDecimal.valueOf(money));
                tbOrderMapper.insertSelective(order);
                for (TbOrderItem orderItem : cartVo.getOrderItemList()) {//商家的明细
                    for (String s : itmeId) {
                        if (Objects.equals(orderItem.getItemId().toString(),s)) {//判断
                            long j = idWorker.nextId();
                            orderItem.setId(j);
                            orderItem.setOrderId(order.getOrderId());
                            orderItem.setOrderNum(String.valueOf(id));
                            orderItemMapper.insertSelective(orderItem);
                        }
                    }
                }
            }
            List<TbOrderItem> orderItemList = cartVo.getOrderItemList();
            for (String s : itmeId) {
                Iterator<TbOrderItem> iterator = orderItemList.iterator();
                while (iterator.hasNext()){
                    TbOrderItem next = iterator.next();
                    if(next.getItemId().toString().equals(s)){
                        iterator.remove();
                    }
                }
            }
        }
        Iterator<CartVo> iterator = cartList.iterator();
        while (iterator.hasNext()){
            CartVo next = iterator.next();
            if(next.getOrderItemList().size() == 0){
                iterator.remove();
            }
        }
        redisTemplate.boundHashOps("cartList").put(user.getId(),cartList);
        return he;
    }

    @Override
    public void updateOrderItemById(String out_biz_no) {
        TbOrderItemExample example = new TbOrderItemExample();
        TbOrderItemExample.Criteria criteria = example.createCriteria();
        String trim = out_biz_no.trim();
        System.out.println(trim);
        criteria.andIdEqualTo(Long.valueOf(trim));
        List<TbOrderItem> items = orderItemMapper.selectByExample(example);
        for (TbOrderItem item : items) {
            item.setStatus("3");
            orderItemMapper.updateByPrimaryKeySelective(item);
        }
    }

    @Override
    public String selectUserIdByOrderNumandTradeNum(String out_trade_no, String trade_no) {
        return tbOrderMapper.selectUserIdByOrderNumandTradeNum(out_trade_no,trade_no);
    }

    @Override
    public List<OrderVo> getSendListByUsername(String username) {
        List<OrderVo> list = tbOrderMapper.selectByUsernameAndStatus(username);
        for (OrderVo orderVo : list) {
            TbOrderItemExample example =  new TbOrderItemExample();
            TbOrderItemExample.Criteria criteria = example.createCriteria();
            criteria.andOrderIdEqualTo(orderVo.getOrderId());
            List<TbOrderItem> items = orderItemMapper.selectByExample(example);
            for (TbOrderItem item : items) {
                item.setsId(String.valueOf(item.getId()));
            }
            orderVo.setItemList(items);
        }
        return list;
    }

    @Override
    @Transactional
    public void updateStatusByOrderNumAndItemOrder(String out_trade_no,String trade_no) {
        orderItemMapper.updateTradeNumByOrderNum(out_trade_no,trade_no);
        tbOrderMapper.updateStatusAndTradeNumByOrderNum(out_trade_no,trade_no);
    }

    public static void main(String[] args) {
        String s = "1585243872865288192 ";
        String trim = s.trim();
        Long l = Long.valueOf(trim);
        System.out.println(l);
    }
}
